package com.kellyfeng.test.dom4j;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

//import javax.swing.plaf.synth.SynthSeparatorUI;

//import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
//import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 * @author Kellyfeng
 *
 */
public class Dom4jImpl implements Dom4j {

	/* (non-Javadoc)
	 * @see com.kellyfeng.test.dom4j.Dom4j#write()
	 */
	@Override
	public void write() throws IOException {
		Document document = DocumentHelper.createDocument();
		Element root = document.addElement("root");

		@SuppressWarnings("unused")
		Element author1 = root.addElement("author").addAttribute("name", "James").addAttribute("location", "UK")
				.addText("James Strachan");

		@SuppressWarnings("unused")
		Element author2 = root.addElement("author").addAttribute("name", "Bob").addAttribute("location", "US")
				.addText("Bob McWhirter");
		
		System.out.println(document.asXML());
		
		// lets write to a file
		XMLWriter writer = new XMLWriter(new FileWriter("D:/abc.xml"));
		writer.write(document);
		writer.close();

//		// Pretty print the document to System.out
//		OutputFormat format = OutputFormat.createPrettyPrint();
//		writer = new XMLWriter(System.out, format);
//		writer.write(document);
//
//		// Compact format to System.out
//		format = OutputFormat.createCompactFormat();
//		writer = new XMLWriter(System.out, format);
//		writer.write(document);
	}

	/* (non-Javadoc)
	 * @see com.kellyfeng.test.dom4j.Dom4j#read()
	 */
	@Override
	public void read() throws DocumentException {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("D:/abc.xml"));
		Element root = document.getRootElement();

		// iterate through child elements of root
		for (Iterator<?> i = root.elementIterator(); i.hasNext();) {
			Element element = (Element) i.next();
			System.out.println(element.getName() + ":" + element.getText());
		}

//		// iterate through child elements of root with element name "foo"
//		for (Iterator<?> i = root.elementIterator("foo"); i.hasNext();) {
//			Element foo = (Element) i.next();
//			// do something
//		}
//
//		// iterate through attributes of root
//		for (Iterator<?> i = root.attributeIterator(); i.hasNext();) {
//			Attribute attribute = (Attribute) i.next();
//			// do something
//		}
	}
	
	/**
	 * @param args
	 * @throws IOException
	 * @throws DocumentException
	 */
	public static void main(String[] args) throws IOException, DocumentException {
		Dom4jImpl dom = new Dom4jImpl();
		dom.write();
		System.out.println("~~~~~~~~~~~Writed");
		dom.read();
		System.out.println("~~~~~~~~~~~Readed");
		Runtime.getRuntime().exec("cmd /c del D:\\abc.xml");
		System.out.println("~~~~~~~~~~~Deleted");
	}

}
